package org.hong.dao;

import org.hong.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl implements UserDao{
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public List<User> FindAll() {
        String sql = "select * from user";
        List<User> users = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class));
        return users;

    }

    @Override
    public int insertUser(User user) {

        String sql = "insert into user(username, password,identity) values ( ?, ? , ?)";

        int i = jdbcTemplate.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getIdentity()
               );
        return i;
    }

    @Override
    public User findByName(User user) {
        String sql = "SELECT * FROM user WHERE username = ?";
        User users = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), user.getUsername());
        return users;
    }


    @Override
    public User findByPassword(User user) {
        String sql = "select * from user where password = ?";
        User users = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),user.getPassword());
        return users;
    }
}


